package com.qust.endecryptionmanager.service.imp;

import com.qust.endecryptionmanager.pojo.ZkNode;
import com.qust.endecryptionmanager.service.LoadBalanceService;
import com.qust.endecryptionmanager.utils.loadBalance.WRR;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class LoadBalanceServiceImp implements LoadBalanceService {
    private final Map<String, ZkNode> zkNodes;

    public LoadBalanceServiceImp(Map<String, ZkNode> zkNodes) {
        this.zkNodes = zkNodes;
    }

    @Override
    public List<String> getConnectAddress(int number) {
        List<String > result = new ArrayList<>();
        WRR wrr = new WRR(new ArrayList<>(zkNodes.values()));

        for (int i = 0; i < number; i++) {
            result.add(wrr.select().getAddress());
        }
        return result;
    }
}
